package com.iwhalecloud.bss.kite.cucc.service.attr.handler;

import com.alibaba.fastjson.JSONObject;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.manager.api.IOperateSceneInstService;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import com.iwhalecloud.bss.kite.service.attr.handler.AbstractAttrHandler;
import com.ztesoft.bss.base.util.LocalAttrUtils;
import com.ztesoft.bss.common.util.ContextUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * 网格处理类
 */
public class GoEnterpriseOrgAttrHandler extends AbstractAttrHandler<KiteProdInst, AttrData> {

    @Autowired
    IOperateSceneInstService operateSceneInstService;
    @Autowired
    private IQuerySceneInstService querySceneInstService;

    @Override
    public void onShow(KiteAttr attr, KiteProdInst inst, AttrData data){
        //        业务变更初始化不校验

        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String BUSI_CHANGE_SCENE_ONE = querySceneInstService.getExtParameterValue(data.getSceneInstId(), "BUSI_CHANGE_SCENE_ONE");
        if (null!=BUSI_CHANGE_SCENE_ONE&&KiteStringUtils.isEqual(BUSI_CHANGE_SCENE_ONE, LocalKeyConsts.IFTRUE_T)) {
            if (LocalKeyConsts.PROVINCE_ZHEJIANG.equals(provinceCode)&&null==attr.getValue()) {
                attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_H);
            }
        }
        //极简受理-标记是否已进入业务变更场景：T-业务变更，其他情况(F,或者空)-其他业务场景
        String BUSI_CHANGE = querySceneInstService.getExtParameterValue(data.getSceneInstId(), LocalKeyConsts.BUSI_CHANGE);
        if (null!=BUSI_CHANGE&&KiteStringUtils.isEqual(BUSI_CHANGE, LocalKeyConsts.IFTRUE_T)) {
            if (LocalKeyConsts.PROVINCE_ZHEJIANG.equals(provinceCode)) {
                KiteAttr attrByInst = inst.getAttr(attr.getFieldName());
                attrByInst.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_M);
                attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_M);
            }
        }

        if (null!=attr.getValue()) {
            attr.setShowType(KeyConsts.PROD_ATTR_SHOW_TYPE_M);
        }
        super.onShow(attr,inst,data);
    }
    @Override
    public void after(KiteAttr attr, KiteProdInst inst, AttrData data) {
        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        String cityCode = ContextUtil.getLoginInfo().getUserInfo().getPostLanId();
        List<KiteAttrValue> attrValues = new ArrayList<>();
        attrValues.add(LocalAttrUtils.pleaseSelect());
        if (LocalKeyConsts.PROVINCE_TIANJIN.equals(provinceCode)) {
            attr.setAttrValueTypeId("98A");
            attr.setShowType("D");
            //如果当前组织是网格，则查询组织信息，从组织信息中获取县分填充
            JSONObject result= LocalAttrUtils.getInstance().queryOrgInfo(null,provinceCode,cityCode,LocalKeyConsts.W56622D);
            if(null!=result.getJSONArray("LIST")&&result.getJSONArray("LIST").size()>0) {
                List<JSONObject> orgInfoList = result.getJSONArray("LIST").toJavaList(JSONObject.class);
                for (JSONObject orgInfo : orgInfoList) {
                    KiteAttrValue kiteAttrValue = new KiteAttrValue();
                    kiteAttrValue.setAttrValue(orgInfo.getString("NAME"));
                    kiteAttrValue.setAttrValueName(orgInfo.getString("NAME"));
                    kiteAttrValue.setAttrValueDesc(orgInfo.getString("ORG_CODE"));
                    kiteAttrValue.setAttrId(attr.getAttrId());
                    kiteAttrValue.setSortBy("1");
                    attrValues.add(kiteAttrValue);
                }
                attr.setAttrValues(attrValues);

            }
            attr.setIsNull(KeyConsts.IFTRUE_F);
        }

        super.after(attr, inst, data);
    }
}
